package com.panghl.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author panghl
 * @Date 2022/2/12
 * @Description TODO
 **/
@RestController
public class HelloController {

    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/hello")
    public String hello(){
        return "hasAuthority('hello')";
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/admin")
    public String admin(){
        return "hasRole('admin')";
    }
    @PreAuthorize("hasAuthority('system:test:list')")
    @GetMapping("/h1")
    public String h1(){
        return "h1";
    }

    @PreAuthorize("eg.hasPermission('system:test:list')")
    @GetMapping("/diyEgPermission")
    public String diyEgPermission(){
        return "自定义权限校验";
    }


    /**
     * 获取当前登录用户
     */
    @GetMapping("/loginUser")
    @ResponseBody
    public UserDetails getCurrentUser(){
        return (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 获取当前登录用户2
     */
    @GetMapping("/loginUser2")
    @ResponseBody
    public UserDetails getCurrentUser2(Authentication authentication){
        return (UserDetails) authentication.getPrincipal();
    }

    /**
     * 获取当前登录用户
     */
    @GetMapping("/loginUser3")
    @ResponseBody
    public UserDetails getCurrentUser3(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

}
